<?php

namespace app\admin\controller\user;

use app\admin\controller\AdminController;
use app\admin\service\user\UserService;
use app\admin\validate\file\ExportValidate;
use app\admin\validate\user\UserValidate;
use think\response\Json;

/**
 * 用户管理控制器
 */
class User extends AdminController
{

    /**
     * 查看用户列表
     * @return Json
     */
    public function lists()
    {
        return $this->data(UserService::getList());
    }

    /**
     * 添加用户
     * @return Json
     */
    public function add()
    {
        $params = (new UserValidate())->post()->checkParam('add');
        $params['admin_id'] = $this->adminId;
        $result =  UserService::add($params);
        if(true === $result) {
            return $this->success([], '添加成功');
        } else {
            return $this->error(UserService::getError());
        }
    }

    /**
     * 编辑用户
     * @return Json
     */
    public function edit()
    {
        $params = (new UserValidate())->post()->checkParam('edit');
        $params['admin_id'] = $this->adminId;
        $result = UserService::edit($params);
        if(true === $result) {
            return $this->success([], '编辑成功');
        } else {
            return $this->error(UserService::getError());
        }
    }

    /**
     * 用户详情
     * @return Json
     */
    public function detail()
    {
        $params = (new UserValidate())->checkParam('detail');
        $result = UserService::detail($params);
        return $this->data($result);
    }

    /**
     * 用户token管理
     * @return Json
     */
    public function token()
    {
        $params = (new UserValidate())->checkParam('token');
        $result = UserService::tokenInfo($params);
        return $this->data($result);
    }

    /**
     * 用户token管理
     * @return Json
     */
    public function deleteToken()
    {
        $params = (new UserValidate())->checkParam('deleteToken');
        $result = UserService::deleteToken($params);
        return $this->data($result);
    }

    /**
     * 更改用户状态
     * @return Json
     */
    public function updateStatus()
    {
        $params = (new UserValidate())->post()->checkParam('updateStatus');
        $result = UserService::updateStatus($params);
        if(true === $result) {
            return $this->success([], '修改成功');
        } else {
            return $this->error(UserService::getError());
        }
    }

    /**
     * 用户列表导出
     * @return Json
     */
    public function export()
    {
        $params = (new ExportValidate())->post()->checkParam('export');
        $result = UserService::export($params,$this->adminInfo);
        if(false !== $result) {
            return $this->data($result);
        } else {
            return $this->error(UserService::getError());
        }
    }

}